package uutalk.netcloud.talk.app.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.alibaba.fastjson.JSON;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import uutalk.netcloud.talk.R;
import uutalk.netcloud.talk.app.ui.adapter.SigleAdapter;
import uutalk.netcloud.talk.base.android.BaseFragment;
import uutalk.netcloud.talk.base.mqtt.MQTTManager;
import uutalk.netcloud.talk.base.mqtt.PublishFactory;
import uutalk.netcloud.talk.base.mqtt.event.MqttMessageEvent;
import uutalk.netcloud.talk.base.mqtt.model.Publication;
import uutalk.netcloud.talk.base.util.LanguageUtil;
import uutalk.netcloud.talk.base.util.thirdpart.UserManager;
import uutalk.netcloud.talk.base.util.view.ToastUtil;
import uutalk.netcloud.talk.model.BussinessBeanFactory;
import uutalk.netcloud.talk.model.bean.CMDConstant;
import uutalk.netcloud.talk.model.bean.DBEntityManager;
import uutalk.netcloud.talk.model.bean.FriendInfoBean;
import uutalk.netcloud.talk.model.bean.FriendListBean;
import uutalk.netcloud.talk.model.bean.GroupListWrap;

/**
 * 类描述：
 * 作者：Pengmutian
 * 时间：2018/5/11
 */
public class SigleFragment extends BaseFragment{

    String userID = "";

    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;

    public static SigleFragment newInstance() {
        Bundle args = new Bundle();
        SigleFragment fragment = new SigleFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public boolean supportActionbar() {
        return true;
    }

    @Override
    public boolean isAutoBindEvent() {
        return true;
    }

    @Override
    protected int setLayoutResId() {
        return R.layout.fragment_sigle;
    }

    @Override
    protected void loadData() {

    }

    @Override
    protected void init() {
        getTitleTV().setText(LanguageUtil.getStringById(R.string.single_talk));
        userID = UserManager.getInstance().getUserInfo().getUser_id();
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false));
        recyclerView.addItemDecoration(new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL));

        progressHudUtil.showProgressHud();
        Publication publication = PublishFactory.createPublication(BussinessBeanFactory.createGetSigleFriendList(UserManager.getInstance().getUserInfo().getUser_id()));
        MQTTManager.getInstance().publish(publication);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMqttMsgEvent(MqttMessageEvent event) {
        if(CMDConstant.GET_SIGLE_FRIEND_LIST_RESP.equals(event.getMessage().getCmd())){
            Log.e(TAG, "获取好友列表成功"+event.getMessage().toString());
            progressHudUtil.dismissProgressHud();
            FriendListBean friendListBean = JSON.parseObject(event.getMessage().toString(), FriendListBean.class);
            List<FriendInfoBean> infoBeanList = friendListBean.getFriend_list();

            if(infoBeanList == null  || infoBeanList.size() == 0 ){

            } else {
                Collections.sort(infoBeanList);
                SigleAdapter sigleAdapter = new SigleAdapter(infoBeanList);
                recyclerView.setAdapter(sigleAdapter);
                int size = infoBeanList.size();
                for(int i=0; i<size; i++){
                    DBEntityManager.saveFriendInfoBean(infoBeanList.get(i));
                }
            }
        }
    }
}
